﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AutoClickAdfLy
{
    public partial class FormMain : Form
    {
        List<Auto_URL> listURL;
        int indexCurrentURL = 0;
        int countClick = 0;

        public FormMain()
        {
            InitializeComponent();
            pictureBox1.Visible = false;
            dataGridViewURL.AutoGenerateColumns = false;
            getListURL();
            webBrowserContent.ScriptErrorsSuppressed = true;
            webBrowserContent.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowserContent_DocumentCompleted);
        }

        void webBrowserContent_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            pictureBox1.Visible = false;
            if (webBrowserContent.Url.ToString().Contains("adf.ly"))
            {
                toolStripStatusLabelThongBao.Text = "Đã load xong";
                timerRun.Stop();
                timerClick.Start();
            }
        }

        void getListURL()
        {
            buttonStart.Enabled = buttonStop.Enabled = false;
            try
            {
                using (AutoDBEntities db = new AutoDBEntities())
                {
                    listURL = (from e in db.Auto_URL
                               where e.activated == true
                               select e).ToList();
                    if (listURL != null && listURL.Count > 0)
                    {
                        dataGridViewURL.DataSource = listURL;
                        buttonStart.Enabled = buttonStop.Enabled = true;
                        toolStripStatusLabelThongBao.Text = "Đã lấy danh sách URL thành công";
                    }
                    else
                    {
                        toolStripStatusLabelThongBao.Text = "Không tìm thấy URL nào";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi tải danh sách URL. Chi tiết: " + ex.Message);
            }
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            toolStripStatusLabelThongBao.Text = "Bắt đầu Click tự động";
            buttonStart.Enabled = false;
            buttonRefresh.Enabled = false;
            timerRun.Start();
        }

        private void timerRun_Tick(object sender, EventArgs e)
        {
            timerRun.Stop();
            //Lấy một URL trong datagridview
            string url = listURL[indexCurrentURL].url;
            dataGridViewURL.Rows[indexCurrentURL].Selected = true;
            textBoxURL.Text = url;
            //Mở URL
            if (string.IsNullOrEmpty(url))
            {
                toolStripStatusLabelThongBao.Text = "Danh sách URL rỗng";
                return;
            }
            else
            {
                toolStripStatusLabelThongBao.Text = "Đang chạy";
                webBrowserContent.Navigate(url);
                pictureBox1.Visible = true;
            }

            //Điều chỉnh chỉ số URL
            if (indexCurrentURL == (listURL.Count - 1))
                indexCurrentURL = 0;
            else
                indexCurrentURL++;
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            try
            {
                buttonStart.Enabled = true;
                buttonRefresh.Enabled = true;
                timerRun.Stop();
                timerClick.Stop();
                toolStripStatusLabelThongBao.Text = "Đã dừng";
            }
            catch (Exception ex)
            {
                toolStripStatusLabelThongBao.Text = "Lỗi dừng hoạt động. Chi tiết: " + ex.Message;
            }
        }

        private void timerClick_Tick(object sender, EventArgs e)
        {
            HtmlElement element = webBrowserContent.Document.GetElementById("skip_button");
            if (element != null)
            {
                countClick++;
                element.InvokeMember("click");
                toolStripStatusLabelThongBao.Text = "Đang click tự động";
                toolStripStatusLabelSoClick.Text = "Số click: " + countClick + ".";
                timerClick.Stop();
                timerRun.Start();
            }
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            getListURL();
        }
    }
}
